package com.ruoyi.gxs.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.gxs.domain.*;
import com.ruoyi.gxs.mapper.GxsComprehensiveMapper;
import com.ruoyi.gxs.mapper.GxsSubtypeMapper;
import com.ruoyi.gxs.mapper.GxsYearnameMapper;
import com.ruoyi.gxs.service.*;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsDeductionMapper;

/**
 * 减分Service业务层处理
 * 
 * @author 高晓松
 * @date 2023-12-27
 */
@Service
public class GxsDeductionServiceImpl implements IGxsDeductionService 
{
    @Autowired
    private GxsDeductionMapper gxsDeductionMapper;

    @Autowired
    private IGxsSubtypeService gxsSubtypeService;

    @Autowired
    private GxsComprehensiveMapper gxsComprehensiveMapper;

    @Autowired
    private IGxsStudyChangeService gxsStudyChangeService;

    @Autowired
    private IGxsMoralityService gxsMoralityService;

    @Autowired
    private IGxsSportsService gxsSportsService;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private GxsComprehensiveMapper comprehensiveMapper;

    @Autowired
    private GxsYearnameMapper gxsYearnameMapper;

    /**
     * 查询减分
     * 
     * @param gxsId 减分主键
     * @return 减分
     */
    @Override
    public GxsDeduction selectGxsDeductionByGxsId(Long gxsId)
    {
        return gxsDeductionMapper.selectGxsDeductionByGxsId(gxsId);
    }

    /**
     * 查询减分列表
     * 
     * @param gxsDeduction 减分
     * @return 减分
     */
    @Override
    public List<GxsDeduction> selectGxsDeductionList(GxsDeduction gxsDeduction)
    {
        return gxsDeductionMapper.selectGxsDeductionList(gxsDeduction);
    }

    /**
     * 新增减分
     * 
     * @param gxsDeduction 减分
     * @return 结果
     */
    @Override
    public int insertGxsDeduction(GxsDeduction gxsDeduction)
    {

        //定义减分类型，并查询减分分数
        GxsSubtype gxsSubtype = new GxsSubtype();
        gxsSubtype.setGxsSubtypeId(gxsDeduction.getGxsSubtypeId());
        gxsSubtype.setGxsParenttypeId(gxsDeduction.getGxsParenttypeId());
        List<GxsSubtype> gxsSubtypes = gxsSubtypeService.selectGxsSubtypeList(gxsSubtype);
        Long deduction = gxsSubtypes.get(0).getGxsDeduction();

        //查看综测类型
        Long gxsParenttypeId = gxsDeduction.getGxsParenttypeId();

        //专业成绩
        if(gxsParenttypeId == 1L) {

            //根据学号查询 昵称、用户ID、部门 并添加到gxsStudyChange
            SysUser sysUser = sysUserMapper.selectUserByUserName(gxsDeduction.getUserName());

            GxsStudyChange studyChange = new GxsStudyChange();

            studyChange.setNickName(sysUser.getNickName());
            studyChange.setUserId(sysUser.getUserId());
            studyChange.setDeptId(sysUser.getDeptId());
            studyChange.setUserName(sysUser.getUserName());

            //根据新增的专业分数信息 查询综合得分学习成绩
            GxsComprehensive com = new GxsComprehensive();
            com.setUserName(gxsDeduction.getUserName());
            com.setGxsYear(gxsDeduction.getGxsYear());
            List<GxsComprehensive> gxsComprehensives = comprehensiveMapper.selectGxsComprehensiveList(com);
            GxsComprehensive comprehensive = gxsComprehensives.get(0);

            Long gxsYear = gxsDeduction.getGxsYear();
            GxsYearname gxsYearname = gxsYearnameMapper.selectGxsYearnameByGxsYearid(gxsYear);
            studyChange.setGxsYear(gxsYearname.getGxsYearid());

            studyChange.setGxsSchoolrecord(comprehensive.getGxsSchoolrecord());
            studyChange.setGxsBonusState(1L);
            studyChange.setGxsScore(deduction);

            studyChange.setGxsAddCause("扣分" + deduction);

            studyChange.setGxsTotalPoints(comprehensive.getGxsSchoolrecord() - deduction);

            gxsStudyChangeService.insertGxsStudyChange(studyChange);
        }

        //思想品德
        if(gxsParenttypeId == 2L) {
            GxsMorality morality = new GxsMorality();

            //根据学号查询 昵称、用户ID、部门 并添加到gxsStudyChange
            SysUser sysUser = sysUserMapper.selectUserByUserName(gxsDeduction.getUserName());

            morality.setNickName(sysUser.getNickName());
            morality.setUserId(sysUser.getUserId());
            morality.setDeptId(sysUser.getDeptId());
            morality.setUserName(sysUser.getUserName());

            //根据新增的专业分数信息 查询综合得分学习成绩
            GxsComprehensive com = new GxsComprehensive();
            com.setUserName(gxsDeduction.getUserName());
            com.setGxsYear(gxsDeduction.getGxsYear());
            List<GxsComprehensive> gxsComprehensives = comprehensiveMapper.selectGxsComprehensiveList(com);
            GxsComprehensive comprehensive = gxsComprehensives.get(0);

            Long gxsYear = gxsDeduction.getGxsYear();
            GxsYearname gxsYearname = gxsYearnameMapper.selectGxsYearnameByGxsYearid(gxsYear);
            morality.setGxsYear(gxsYearname.getGxsYearid());

            morality.setGxsMoralityRecord(comprehensive.getGxsThought());
            morality.setGxsBonusState(1L);
            morality.setGxsScore(deduction);

            morality.setGxsAddCause("扣分" + deduction);

            morality.setGxsTotalPoints(comprehensive.getGxsSchoolrecord() - deduction);

            gxsMoralityService.insertGxsMorality(morality);

        }

        //体美劳
        if(gxsParenttypeId == 3L) {
            GxsSports sports = new GxsSports();

            //根据学号查询 昵称、用户ID、部门 并添加到gxsStudyChange
            SysUser sysUser = sysUserMapper.selectUserByUserName(gxsDeduction.getUserName());

            sports.setNickName(sysUser.getNickName());
            sports.setUserId(sysUser.getUserId());
            sports.setDeptId(sysUser.getDeptId());
            sports.setUserName(sysUser.getUserName());

            //根据新增的专业分数信息 查询综合得分学习成绩
            GxsComprehensive com = new GxsComprehensive();
            com.setUserName(gxsDeduction.getUserName());
            com.setGxsYear(gxsDeduction.getGxsYear());
            List<GxsComprehensive> gxsComprehensives = comprehensiveMapper.selectGxsComprehensiveList(com);
            GxsComprehensive comprehensive = gxsComprehensives.get(0);

            Long gxsYear = gxsDeduction.getGxsYear();
            GxsYearname gxsYearname = gxsYearnameMapper.selectGxsYearnameByGxsYearid(gxsYear);
            sports.setGxsYear(gxsYearname.getGxsYearid());

            sports.setGxsScore(comprehensive.getGxsThought());
            sports.setGxsBonusState(1L);
            sports.setGxsScore(deduction);

            sports.setGxsAddCause("扣分" + deduction);

            sports.setGxsTotalPoints(comprehensive.getGxsSchoolrecord() - deduction);

            gxsSportsService.insertGxsSports(sports);
        }

        return gxsDeductionMapper.insertGxsDeduction(gxsDeduction);
    }

    /**
     * 修改减分
     * 
     * @param gxsDeduction 减分
     * @return 结果
     */
    @Override
    public int updateGxsDeduction(GxsDeduction gxsDeduction)
    {
        return gxsDeductionMapper.updateGxsDeduction(gxsDeduction);
    }

    /**
     * 批量删除减分
     * 
     * @param gxsIds 需要删除的减分主键
     * @return 结果
     */
    @Override
    public int deleteGxsDeductionByGxsIds(Long[] gxsIds)
    {
        return gxsDeductionMapper.deleteGxsDeductionByGxsIds(gxsIds);
    }

    /**
     * 删除减分信息
     * 
     * @param gxsId 减分主键
     * @return 结果
     */
    @Override
    public int deleteGxsDeductionByGxsId(Long gxsId)
    {
        return gxsDeductionMapper.deleteGxsDeductionByGxsId(gxsId);
    }
}
